package com.kuaifu.autofare.common.utils;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.alibaba.fastjson.JSONObject;
import com.kuaifu.autofare.common.constants.CommonConstatns;

/**
 * 加密
 *
 * @author lishi
 * @date 2017-12-18 上午11:28:15
 */
public class EncryptUtil {

    public static final String KEY_ALGORITHM = "AES";
    public static final String CIPER_ALGORITHM = "AES";

    /**
     * 加密
     *
     * @param content  需要加密的内容
     * @param password 加密密码
     * @return
     */
    public static String encrypt(String content, String password) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(password.getBytes());
        kgen.init(128, secureRandom);
        
        //kgen.init(128, new SecureRandom(password.getBytes()));
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPER_ALGORITHM);// 创建密码器
        byte[] byteContent = content.getBytes("utf-8");
        cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
        byte[] result = cipher.doFinal(byteContent);
        return parseByte2HexStr(result);
    }

    public static String encrypt(String contentd) throws Exception {
        return encrypt(contentd, CommonConstatns.AES_KEY);
    }

    /**
     * 解密
     *
     * @param content  待解密内容
     * @param password 解密密钥
     * @return
     */
    public static String decrypt(String String, String password) throws Exception {
        byte[] content = parseHexStr2Byte(String);
        KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(password.getBytes());
        kgen.init(128, secureRandom);

        //kgen.init(128, new SecureRandom(password.getBytes()));
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPER_ALGORITHM);// 创建密码器
        cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
        byte[] result = cipher.doFinal(content);
        return new String(result);
    }

    public static String decrypt(String string) throws Exception {
        return decrypt(string, CommonConstatns.AES_KEY);
    }

    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    public static void main(String[] args) throws Exception {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("uri", "/user/recharge/topup");
        jsonObj.put("userId", "1");
        String orginStr = jsonObj.toJSONString();
        String afterEncrypt = encrypt(orginStr, CommonConstatns.AES_KEY);
        String afterDecrypt = decrypt(afterEncrypt, CommonConstatns.AES_KEY);
        System.out.println("orginStr : " + orginStr);
        System.out.println("afterEncrypt : " + afterEncrypt);
        System.out.println("afterDecrypt : " + afterDecrypt);

        System.out.println(orginStr == afterDecrypt);
        System.out.println(orginStr.equals(afterDecrypt));
    }

}
